package com.credit.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.credit.entity.CreditPhoneSection;
import com.credit.entity.CreditUserIdentify;
import com.credit.entity.CreditUserInfo;
import com.credit.exception.ErrorException;
import com.credit.repository.CreditPhoneSectionDao;
import com.credit.repository.CreditUserIdentifyDao;
import com.credit.repository.UserDao;
import com.credit.util.CodeValueUtil;
import com.credit.util.Constants;
import com.credit.util.MD5Util;
import com.credit.util.PointsUtil;

@Service("userService")
@Transactional
public class UserService {
	
	private static final String USER_REDIS_PRIFX = "USERID_";

	@Autowired
	private RedisService redisService;
	@Autowired
	private UserDao userDao;
	@Autowired
	private CreditPhoneSectionDao creditPhoneSectionDao;
	@Autowired
	private CreditUserIdentifyDao creditUserIdentifyDao;
	
	public CreditUserInfo login(String mobile, String password) throws ErrorException {
		
		CreditUserInfo userInfo = userDao.findByMobile(mobile);
		
		if(userInfo == null){
			throw new ErrorException(Constants.USER_NOTEXISTS_ERROR_CODE, Constants.USER_NOTEXISTS_ERROR_DESC);
		}
		
		if(!userInfo.getPassword().equalsIgnoreCase(MD5Util.MD5(password))){
			throw new ErrorException(Constants.PASSWORD_NOTCORRECT_ERROR_CODE, Constants.PASSWORD_NOTCORRECT_ERROR_DESC);
		}
		
		return userInfo;
	}

	public CreditUserInfo register(String mobile, String password,Integer userType) throws ErrorException {
		
		CreditPhoneSection phoneSection = creditPhoneSectionDao.findByPhone(mobile.substring(0, 7));
		
		CreditUserInfo userInfo = new CreditUserInfo();
		userInfo.setMobile(mobile);
		userInfo.setPassword(MD5Util.MD5(password));
		userInfo.setNickName(mobile);
		userInfo.setUserType(userType);
		userInfo.setMobileCheck(CodeValueUtil.MOBILE_CHECK);
		userInfo.setCreateTime(new Date());
		userInfo.setUpdateTime(new Date());
		userInfo.setCoin(0);
		userInfo.setScore(0);
		userInfo.setLevel("Lv.0");
		userInfo.setEvaluate(0);
		userInfo.setIdentifyCheck(CodeValueUtil.IDENTIFY_NOTCHECK);
		userInfo.setSesameCredit(0);
		userInfo.setSocialSecCheck(CodeValueUtil.SOCIALSEC_NOTCHECK);
		userInfo.setJingdongCheck(CodeValueUtil.JINGDONG_NOTCHECK);
		//设置手机号归属地
		userInfo.setAttribution(phoneSection.getCity()+" "+phoneSection.getIsp());
		userInfo.setScore(userInfo.getScore()+5);  //注册+5积分
		CreditUserInfo returnUser = updateUserInfo(userInfo);
		
		//添加积分日志
		PointsUtil.addScoreLog(userInfo.getId(),5,"注册成功");
		
		return returnUser;
		
	}

	public CreditUserInfo forgetPass(String mobile, String password) throws ErrorException {
       CreditUserInfo userInfo = userDao.findByMobile(mobile);
		
		if(userInfo==null){
			throw new ErrorException(Constants.USER_NOTEXIST_ERROR_CODE, Constants.USER_NOTEXIST_ERROR_DESC);
		}
		
		userInfo.setPassword(MD5Util.MD5(password));
		userInfo.setUpdateTime(new Date());
		updateUserInfo(userInfo);
		
		return userInfo;
	}

	public void identify(CreditUserInfo userInfo) {
		
		updateUserInfo(userInfo);
		
	}

	public CreditUserInfo findUserById(Long userId) {
		
		CreditUserInfo userInfo = (CreditUserInfo) redisService.getValue(USER_REDIS_PRIFX+userId);
		
		if(userInfo == null){
			userInfo = userDao.findOne(userId);
			redisService.setValueDefaultNull(USER_REDIS_PRIFX+userId, userInfo);
		}
		
		return userInfo;
	}

	public CreditUserInfo updateUserInfo(CreditUserInfo userInfo) {
		userInfo.setUpdateTime(new Date());
		//修改缓存
		//redisService.setValueDefaultNull(USER_REDIS_PRIFX+userInfo.getId(), userInfo);
		
		//修改后直接删除
		redisService.delete(USER_REDIS_PRIFX+userInfo.getId());
		
		return userDao.save(userInfo);
	}

	public List<Long> findUserByCurrentCity(String currentCity) {
		
		return userDao.findUserByCurrentCity(currentCity);
	}

	public CreditUserInfo findUserByMobile(String mobile) {
		
		return userDao.findByMobile(mobile);
	}

	public void authUser(CreditUserIdentify identify, CreditUserInfo userInfo) {
		
		updateUserInfo(userInfo);
		
		creditUserIdentifyDao.save(identify);
		
	}

	public CreditUserInfo findUserByIdNoCache(Long id) {
		
		return userDao.findOne(id);
	}

	public CreditUserIdentify findIdentifyByUId(Long uId) {
		
		return userDao.findIdentifyByUId(uId);
	}
	


	

}
